package cc.rengu.redp.flowable.bizmodel.divide;

import cc.rengu.redp.flowable.bizmodel.db.domain.BizAllCfg;
import cc.rengu.redp.flowable.bizmodel.db.domain.BizCfgAlg;
import cc.rengu.redp.flowable.bizmodel.db.domain.BizCfgField;
import cc.rengu.redp.flowable.bizmodel.db.domain.BizCfgModel;
import cc.rengu.redp.flowable.bizmodel.web.service.BizCfgAlgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BizModelDivider implements Divider {

    @Autowired
    BizCfgAlgService bizCfgAlgService;

    @Override
    public List<BizCfgModel> divideToModels(String procDefId, String nodeId) {
        List<BizAllCfg> bizAllCfgs = bizCfgAlgService.findAllBizCfgByProcId(procDefId, nodeId);
        List<BizCfgModel> bizCfgModels = new ArrayList<>();

        Map<Long, BizCfgModel> modelMap = new HashMap<>();

        bizAllCfgs.forEach(bizAllCfg -> {
            BizCfgModel bcm = modelMap.get(bizAllCfg.getModelId());
            if (bcm == null) {
                bcm = new BizCfgModel();
                //获取Model配置
                bcm.setAuthDepts(bizAllCfg.getAuthDepts());
                bcm.setAuthPluginPath(bizAllCfg.getAuthPluginPath());
                bcm.setAuthFixedField(bizAllCfg.getAuthFixedField());
                bcm.setModelId(bizAllCfg.getModelId());
                bcm.setModelName(bizAllCfg.getModelName());
                bcm.setAuthType(bizAllCfg.getAuthType());
                modelMap.put(bizAllCfg.getModelId(), bcm);
            }

            //获取Field配置
            BizCfgField bcf = null;
            if (bcm.getFieldData() == null) {
                bcm.setFieldData(new ArrayList<>());
            } else {
                for (int i = 0; i < bcm.getFieldData().size(); i++){
                    BizCfgField bizCfgField = bcm.getFieldData().get(i);
                    if (bizCfgField.getFieldId().equals(bizAllCfg.getFieldId())){
                        bcf = bizCfgField;
                    }
                }
            }
            if (bcf == null) {
                bcf = new BizCfgField();
                bcf.setModelId(bizAllCfg.getModelId());
                bcf.setIsIndex(bizAllCfg.getIsIndex());
                bcf.setIsPriKey(bizAllCfg.getIsPriKey());
                bcf.setIsDict(bizAllCfg.getIsDict());
                bcf.setFieldType(bizAllCfg.getFieldType());
                bcf.setFieldDesc(bizAllCfg.getFieldDesc());
                bcf.setFieldName(bizAllCfg.getFieldName());
                bcf.setFatherFieldName(bizAllCfg.getFatherFieldName());
                bcf.setFieldId(bizAllCfg.getFieldId());
                bcm.getFieldData().add(bcf);
            }

            //获取Alg配置
            if (bcf.getAlgData() == null) {
                bcf.setAlgData(new ArrayList<>());
            }
            BizCfgAlg bca = new BizCfgAlg();
            bca.setFieldId(bizAllCfg.getFieldId());
            bca.setAlgDesc(bizAllCfg.getAlgDesc());
            bca.setAlgId(bizAllCfg.getAlgId());
            bca.setAlgType(bizAllCfg.getAlgType());
            bca.setPriKeyAlgDesc(bizAllCfg.getPriKeyAlgDesc());
            bca.setPriKeyAlgType(bizAllCfg.getPriKeyAlgType());
            bca.setProcDefId(bizAllCfg.getProcDefId());
            bcf.getAlgData().add(bca);
        });

        bizCfgModels.addAll(modelMap.values());
        return bizCfgModels;
    }
}
